[numpy] storing record arrays in object arrays

Posted by Peter Prettenhofer on Stack Overflow See other posts from Stack Overflow or by Peter Prettenhofer
Published on 2010-04-14T23:15:45Z Indexed on 2010/04/15 0:53 UTC
Read the original article Hit count: 418

Filed under:
|

I'd like to convert a list of record arrays -- dtype is (uint32, float32) -- into a numpy array of dtype np.object:

X = np.array(instances, dtype = np.object)

where instances is a list of arrays with data type np.dtype([('f0', '<u4'), ('f1', '<f4')]). However, the above statement results in an array whose elements are also of type np.object:

X[0]
array([(67111L, 1.0), (104242L, 1.0)], dtype=object)

Does anybody know why?

The following statement should be equivalent to the above but gives the desired result:

X = np.empty((len(instances),), dtype = np.object)
X[:] = instances
X[0]
array([(67111L, 1.0), (104242L, 1.0), dtype=[('f0', '<u4'), ('f1', '<f4')])

thanks & best regards, peter

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy